package mianshi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 * @author cyt
 * @create 2020-05-15 9:43
 */
public class Test1 {
    // 第一题
    /**
     * 思路：
     * 输入数据
     * 排序， 求和， 判断
     * @param args
     */
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int maxn = scanner.nextInt();
        // 存放数
        ArrayList<Integer> list = new ArrayList<Integer>();
        scanner.nextLine();
        String num = scanner.nextLine();
        String arr[] = num.split(" ");

        for (int i = 0; i < arr.length; i++) {
            list.add(Integer.parseInt(arr[i]));
        }
        // 升序
        Collections.sort(list, (a, b) -> {
            return a - b;
        });
        int sum = 0;
        for (Integer integer : list) {
            sum += integer;
            if (sum > maxn) {
                sum -= integer;
                break;
            }
        }
        System.out.println(sum);
    }
}
